from rest_framework.views import exception_handler
from rest_framework.response import Response
from rest_framework import status
from django.db import DatabaseError


def custom_exception_handler(exc, context):
    # 1,调用系统方法处理异常
    response = exception_handler(exc, context)
    # 2,如果是None表名系统处理不了, 如果不是None表名系统处理了
    if response is not None:
        response.data['status_code'] = response.status_code
    else:
        if isinstance(exc, DatabaseError):
            response = Response('数据库,出血了...', status=status.HTTP_400_BAD_REQUEST)
        else:
            response = Response('其他异常', status=status.HTTP_400_BAD_REQUEST)
    return response
